/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.eit.ntc.lmsbackend.struts.action;


import com.eit.ntc.lmsbackend.struts.form.LMSForm;
import com.eit.ntc.lmsbackend.utils.RoleUtils;
import com.eit.ntc.struts.action.AbstractWebFlowAction;
import com.eit.toschoolonline.lms.jdo.LearningItemM;
import com.eit.toschoolonline.lms.jdo.SubjectM;
import com.eit.toschoolonline.lms.service.DocumentItemService;
import com.eit.toschoolonline.lms.service.LearningItemService;
import com.eit.toschoolonline.lms.service.LessonService;
import com.eit.toschoolonline.lms.service.SubjectService;
import java.util.ArrayList;
import java.util.List;
import org.springframework.webflow.execution.Event;
import org.springframework.webflow.execution.RequestContext;

/**
 *
 * @author chanon
 */
public class ZipXMLMainAction extends AbstractWebFlowAction{
        static final int BUFFER = 2048;
        private LessonService lessonService;
        private SubjectService subjectService;
        private LearningItemService learningItemService;
        private DocumentItemService documentItemService;
       
        public Event MainDoc(RequestContext context) throws Exception {
         
          LMSForm lForm = (LMSForm) getFormObject(context);
          lForm.setSubjectM(null);
          lForm.setLessonM(null);
          lForm.getLessonList().clear();
          lForm.getSubjectList().clear();
          lForm.getEdocList().clear();
          lForm.setSelectSubject("");
          lForm.getItemList().clear();
          lForm.setActiveSelect("");
          lForm.setErrorMode("");
          lForm.setItemM(null);
          lForm.setActiveSelect("");
          SubjectService sservice = getSubjectService();
          lForm.setSubjectList(sservice.getSubjectAll());
          return new Event(this, "success");
        } 
    
    public Event MainZip(RequestContext context) throws Exception {
          LMSForm lForm = (LMSForm) getFormObject(context);
          SubjectService sservice = getSubjectService();
          lForm.setSubjectList(sservice.getSubjectAll());    
          return new Event(this, "success");
        } 
     public Event getItemToZip(RequestContext context) throws Exception {
         LMSForm lForm = (LMSForm) getFormObject(context);
         List zList = lForm.getZipItem();
         String type = "";
         try{
             String[] zindex = getHttpServletRequestParameterValues(context, "iIndex");
             List sumItem = new ArrayList();
             for (int i=0;i<zindex.length;i++){
                 String sIndex = zindex[i];
                 int iIndex = Integer.parseInt(sIndex);
                 LearningItemM xItem = (LearningItemM) zList.get(iIndex);
                 if (xItem.getItemType()==LearningItemM.EDOCUMENT_TYPE){
                     type = calType(type,"1");
                 };
                 if (xItem.getItemType()==LearningItemM.E_ENTRANS_TYPE){
                     type = calType(type,"2");
                 };
                 sumItem.add(xItem);
             };
             setHttpServletSessionAttribute(context,"sumZipItem", sumItem);
             setHttpServletSessionAttribute(context,"zipItemType", type);
            // lForm.setSumZipItem(sumItem);
         } catch(Exception ex){
             ex.printStackTrace();
             return error();
         }
         return success();
     }
     public Event searchItemToZip(RequestContext context) throws Exception {
          LMSForm lForm = (LMSForm) getFormObject(context);
          String subjectCode = lForm.getSelectSubject();
          List zipItems = new ArrayList();
          List typeList = new ArrayList();
          typeList.add(LearningItemM.E_ENTRANS_TYPE);
          typeList.add(LearningItemM.EDOCUMENT_TYPE);
          if (RoleUtils.getRole(context).equals(LearningItemM.INFO_ROLE)){
              //zipItems = learningItemService.loadLearningItemBySubject(subjectCode,typeList);
              zipItems = learningItemService.loadLearningItemBySubject(subjectCode);
              System.out.println("load info");
          } else if (RoleUtils.getRole(context).equals(LearningItemM.NTC_ROLE)){
              zipItems = learningItemService.loadLearningItemBySubject(subjectCode,RoleUtils.getRole(context));
              System.out.println("load ntc");
          };
          
          lForm.setZipItem(zipItems);
          return new Event(this, "success");
        }

    public LessonService getLessonService() {
        return lessonService;
    }

    public void setLessonService(LessonService lessonService) {
        this.lessonService = lessonService;
    }


    public SubjectService getSubjectService() {
        return subjectService;
    }

    public void setSubjectService(SubjectService subjectService) {
        this.subjectService = subjectService;
    }


    public LearningItemService getLearningItemService() {
        return learningItemService;
    }

    public void setLearningItemService(LearningItemService learningItemService) {
        this.learningItemService = learningItemService;
    }

    public DocumentItemService getDocumentItemService() {
        return documentItemService;
    }

    public void setDocumentItemService(DocumentItemService documentItemService) {
        this.documentItemService = documentItemService;
    }

    private String calType(String type, String newType) {
        if (type==null || type.trim().equals("")){
            type = newType;
            return type;
        };
        if (type.equals("3")){
            return type;
        }
        if (type.equals(newType)){
            return type;
        }
        type="3";
        return type;
    }
    

}
